package com.lindroy.androidanimation

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.animation.*
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_tween_anim.*


/**
 * @author Lin
 * @date 2021/3/27
 * @function 视图动画
 * @Description
 */
class TweenAnimActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tween_anim)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.tween_anim, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.item_translate -> translate()
            R.id.item_scale -> scale()
            R.id.item_alpha -> alpha()
            R.id.item_rotate -> rotate()
            R.id.item_group -> group()
        }
        return true
    }

    /**
     * 平移动画
     */
    private fun translate() {
//        val translateAnim = AnimationUtils.loadAnimation(this, R.anim.view_anim_translate)
        /*val translateAnim = TranslateAnimation(
                0F,//起始x坐标
                100F,//结束x坐标
                0F,//起始y坐标
                500F)//结束y坐标*/
        //下面这种方式是设置相对于
        val translateAnim = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0.5F,
            Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 1.0F
        )
        translateAnim.duration = 1500
        button.startAnimation(translateAnim)
        translateAnim.setAnimationListener(object :Animation.AnimationListener{
            override fun onAnimationStart(animation: Animation?) {
                //动画开始
            }

            override fun onAnimationEnd(animation: Animation?) {
                //动画结束
            }

            override fun onAnimationRepeat(animation: Animation?) {
                //动画重复
            }
        })
    }

    /**
     * 放缩动画
     */
    private fun scale() {
        val scaleAnim = AnimationUtils.loadAnimation(this, R.anim.view_anim_scale)
        scaleAnim.duration = 1500
        button.startAnimation(scaleAnim)
    }

    /**
     * 透明动画
     */
    private fun alpha() {
//        val alphaAnim = AnimationUtils.loadAnimation(this, R.anim.view_anim_alpha)
        val alphaAnim = AlphaAnimation(1F, 0F)
        alphaAnim.duration = 1500
        alphaAnim.fillAfter = true //动画结束时不恢复初始状态
        button.startAnimation(alphaAnim)
    }

    /**
     * 旋转动画
     */
    private fun rotate() {
//        val rotateAnim = AnimationUtils.loadAnimation(this, R.anim.view_anim_rotate)
        val rotateAnim = RotateAnimation(
            0F, 360F, Animation.RELATIVE_TO_SELF, 0.5F, Animation
                .RELATIVE_TO_SELF, 0.5F
        )
        rotateAnim.duration = 1500
        button.startAnimation(rotateAnim)
    }

    /**
     * 组合动画
     */
    private fun group() {
//        val anim = AnimationUtils.loadAnimation(this, R.anim.view_anim_group)
//        button.startAnimation(anim)

        //创建动画集合
        val set = AnimationSet(true) //校对器，false表示各个动画使用自己的校对器

        //把动画添加至集合
        //把动画添加至集合
        val translateAnim = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0.5F,
            Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 1.0F
        )
        translateAnim.duration = 1500
        val alphaAnim = AlphaAnimation(1F, 0F)
        alphaAnim.duration = 1500
        alphaAnim.fillAfter = true //动画结束时不恢复初始状态
        button.startAnimation(alphaAnim)
        set.addAnimation(translateAnim)
        set.addAnimation(alphaAnim)

        //开始播放集合
        button.startAnimation(set)
    }
}